#!/usr/bin/perl -w
# Eg,  $ ./foldSchool.pl SCHOOL/*.txt >combinedSchools.txt
# Careful: place the output file in such a way that it
# doesn't become one of the input files!
my %groupName = (
  AL_AL => 'All Students',
  FL_N => 'Non-Low Income',
  FL_Y => 'Low Income',
  GE_01 => 'Male',
  GE_02 => 'Female',
  MG_Y => 'Migrant',
  RA1_01 => 'Native American',
  RA1_02 => 'Black',
  RA1_03 => 'Asian',
  RA1_04 => 'Hispanic',
  RA1_06 => 'White',
  RA1_15 => 'Hawaiian',
  RA1_20 => 'Multi-race',
  SS_FLEP => 'Formerly LEP',
  SS_LEP => 'LEP',
  SS_LEPFLEP => 'LEP/Formerly LEP',
  SS_SPED => 'Special Ed',
  T1_0 => 'Non-Title 1',
  T1_1 => 'Title 1'
);

foreach my $file (@ARGV) {
  $file =~ /(.*\/)?(....)(..)([^.]+)/;
  my $year = $2;
  my $grade = $3;
  my $group = $4;
  open my $in, '<', $file or die "Can't open $file: $!";
  while (<$in>) {
    chomp;
    s/\r//;  # double-chomp
    /^([^\t]+) - ([^\t]+)\t(\d{8})/;
    my $districtName = $1;
    my $schoolName = $2;
    s/^[^\t]+\t//;  # lop off old name to better match field order in district file
    print $year, "\t", $grade, "\t", $groupName{$group}, "\t", 
      $districtName, "\t", $_, "\t", $schoolName, "\n";
  }
  close $in;
}
